﻿sps.provide('sps.gis.supermap.MapControl');
sps.include(sps.baseJsPath + 'libs/supermap/include.js');

sps.gis.supermap.MapControl = function() {
};

sps.gis.supermap.MapControl.prototype = {

    //地图控件初始化，传入地图控件的DIV容器id
    initMap: function (containerDiv) {
        this.map = $create(SuperMap.Web.Mapping.Map, { scales: SVRCFG.MAP_SCALES },
            null, null, $get(containerDiv));
        this.tiledLayer = $create(SuperMap.Web.Mapping.TiledDynamicRESTLayer, {
            url: SVRCFG.MAP_URL,
            transparent: true
        }, {
            initialized: this.tiledLayerInitialized_.bind(this)
        }, null, null);
        //设置默认的主题风格库
        var rawTheme = new SuperMap.Web.Theme.RawTheme();
        this.map.set_theme(rawTheme);
        this.initQueryConfig();
    },

    initScene: function (containerDiv) {
        try {
            //初始化三维场景控件实例,参数为包含控件的HTML元素
            this.scene = new SuperMap.Web.UI.Controls.SceneControl($get(containerDiv), this.sceneInitialized_.bind(this), this.sceneInitializedFaild_);
            return this.scene;
        } catch (e) {
            //若没有安装插件，则抛出该异常
            if (e.name == SuperMap.Web.Realspace.ExceptionName.PlugInNotInstalled) {
                Ext.MessageBox.alert('提示', '<a href="' + SVRCFG.REALSPACE_ICLINET_URL + '">请点击下载三维插件进行安装</a>');
                return null;
            }
            //若使用非IE浏览器，则抛出该异常
            else if (e.name == SuperMap.Web.Realspace.ExceptionName.BrowserNotSupport) {
                Ext.MessageBox.alert('提示', '<p>三维浏览目前只支持IE浏览器</p>');
                return null;
            }
            //抛出其他异常
            else {
                return null;
            }
        }
    },

    //初始化切片图层，将图层加入地图控件中
    tiledLayerInitialized_: function (layer) {
        if (Sys.Browser.name != "Microsoft Internet Explorer") {
            this.featuresLayer = $create(SuperMap.Web.Mapping.SVGLayer, null, null, null, null);
        } else {
            this.featuresLayer = $create(SuperMap.Web.Mapping.VMLLayer, null, null, null, null);
        }
        this.markerLayer = $create(SuperMap.Web.Mapping.MarkerLayer, null, null, null, null);

        this.map.setLayers([layer, this.featuresLayer, this.markerLayer]);

        $create(SuperMap.Web.Mapping.TiledDynamicRESTLayer, {
            url: SVRCFG.OVERVIEW_MAP_URL
        }, {
            initialized: this.overviewMapLayerInitialized_.bind(this)
        }, null, null);
    },

    initQueryConfig: function () {
        Ext.Ajax.request({
            'url': REST_SERVICES_ADDRESS + 'Rest/ProductService.svc/Product',
            'success': function (response, opts) {
                var obj = Ext.decode(response.responseText);
                alert(obj);
            }
        });
    },

    //初始化鹰眼图层，同时加上导航、比例尺等控件
    overviewMapLayerInitialized_: function (layer) {
        var overviewMap = new SuperMap.Web.Controls.OverviewMap({
            'id': 'overviewMap',
            'layer': layer,
            'map': this.map,
            'size': {
                width: 100,
                height: 100
            }
        });

        var compass = new SuperMap.Web.Controls.Compass({
            id: "compass"
        });

        var scaleBar = new SuperMap.Web.Controls.ScaleBar({
            'id': 'scaleBar',
            'scaleBarStyle': {
                'color': 'black',
                'fontSize': 2,
                'mode': 1
            }
        });

        var zoomSlider = new SuperMap.Web.Controls.ZoomSlider({
            'id': 'zoomSlider'
            //'pos': new SuperMap.Web.Core.Point(75, 100)
        });

        this.map.addControls([overviewMap, compass, zoomSlider, scaleBar]);
    },
    //初始化三维控件
    sceneInitialized_: function () {
        var sce = this.scene.get_scene();
        sce.open(SVRCFG.SCENE_URL, SVRCFG.SCENE_NAME);
    },
    sceneInitializedFaild_: function () {
        alert("Realspace initialized failed!");
    },

    //全图
    fullExtent: function () {
        this.map.viewEntire();
    },
    //平移
    pan: function () {
        var panAction = $create(SuperMap.Web.Actions.Pan, {
            map: this.map
        }, null, null, null);
        this.map.set_action(panAction);
    },
    //放大
    zoomIn: function () {
        var zoomInAction = $create(SuperMap.Web.Actions.ZoomIn, {
            map: this.map
        }, null, null, null);
        this.map.set_action(zoomInAction);
    },
    //缩小
    zoomOut: function () {
        var zoomOutAction = $create(SuperMap.Web.Actions.ZoomOut, {
            map: this.map
        }, null, null, null);
        this.map.set_action(zoomOutAction);
    },
    //距离
    measureDistance: function () {

        var drawLineAction = $create(SuperMap.Web.Actions.DrawLine, { map: this.map }, null, null, null);
        drawLineAction.add_actionCompleted(this.measureDrawCompleted_.bind(this));
        this.map.set_action(drawLineAction);
        this.isMeasureDistance = true;

    },
    //面积
    measureArea: function () {
        this.isMeasureDistance = false;
        var drawPolygonAction = $create(SuperMap.Web.Actions.DrawPolygon, {
            map: this.map
        }, null, null, null);
        drawPolygonAction.add_actionCompleted(this.measureDrawCompleted_.bind(this));
        this.map.set_action(drawPolygonAction);
    },
    //量算绘制回调函数
    measureDrawCompleted_: function (
        drawGeometryArgs) {

        this.featuresLayer.clearFeatures();

        var feature = new SuperMap.Web.Core.Feature();
        feature.geometry = drawGeometryArgs.geometry;
        this.featuresLayer.addFeature(feature);

        var unit = new SuperMap.Web.Core.Units().meter;
        var measureParameters = new SuperMap.Web.iServerJava6R.MeasureParameters(drawGeometryArgs.geometry, unit);

        var measureService = new SuperMap.Web.iServerJava6R.MeasureService(SVRCFG.MAP_URL);

        //默认为量算距离，如果量算面积，则需要将 measureMode 设置为 AREA
        if (!this.isMeasureDistance) {
            measureService.set_measureMode(SuperMap.Web.iServerJava6R.MeasureMode.AREA);
        }

        measureService.add_processCompleted(function (measureServiceArgs) {
            var distance = measureServiceArgs.get_result().get_distance();
            var area = measureServiceArgs.get_result().get_area();
            if (distance != -1) {
                Ext.MessageBox.alert('距离', distance + ' ' + '米');
            } else {
                Ext.MessageBox.alert('面积', area + ' ' + '平方米');
            }
        });
        measureService.processAsync(measureParameters);

    },
    clearGraphics: function () {
        this.featuresLayer.clearFeatures();
        var markers = this.markerLayer.get_markers();
        if (markers) {
            for (var i = markers.length - 1; i >= 0; i--) {
                this.markerLayer.removeMarker(markers[i]);
            }
        }
    },
    geometryQuery: function (geometry, queryLayerParams, callback) {

    },
    pointQuery: function (callback) {
        var drawPoint = $create(SuperMap.Web.Actions.DrawPoint, { map: this.map }, null, null, null);
        this.map.set_action(drawPoint);
        drawPoint.add_actionCompleted(function (drawGeometryArgs) {
            var geo = drawGeometryArgs.geometry;
            this.geometryQuery(geo, this.queryConfig.queryLayerParams,
						this.queryResultDisplay);
        });
    }
};